package com.huxun.controller;

import com.huxun.bean.Desk;
import com.huxun.service.DeskService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class DeskController {
    @Autowired
    DeskService deskService;
    @RequestMapping("/desk_query")
    public String query(Desk desk, Model model) {
        List<Desk> desks = deskService.getDesks(desk);
        System.out.println("查询结果:" + desks);
        model.addAttribute("desks", desks);
        //回显搜索内容
        model.addAttribute("desk", desk);
        return "deskList";
    }
    @RequestMapping("/desk_add")
    public void addDesk(Desk desk, HttpServletResponse response) throws Exception{
        //解决中文乱码
        response.setContentType("text/html;charset=utf-8");
        int count = deskService.addDesk(desk);
        if (count>0){
            response.getWriter().write("<script>\n" +
                    "\talert(\"新增餐桌成功\");\n" +
                    "\tlocation.href=\"/desk_query\"\n" +
                    "</script>");
        }else {
            response.getWriter().write("<script>\n" +
                    "\talert(\"新增餐桌失败\");\n" +
                    "\tlocation.href=\"/page_saveDesk\"\n" +
                    "</script>");
        }
    }
    @RequestMapping("/desk_delete/{id}")
    public void deleteDesk(@PathVariable("id") int id, HttpServletResponse response) throws Exception{
        //解决中文乱码
        response.setContentType("text/html;charset=utf-8");
        int count = deskService.deleteDesk(id);
        if (count>0){
            response.getWriter().write("<script>\n" +
                    "\talert(\"删除成功\");\n" +
                    "\tlocation.href=\"/desk_query\"\n" +
                    "</script>");
        }else {
            response.getWriter().write("<script>\n" +
                    "\talert(\"删除失败\");\n" +
                    "\tlocation.href=\"/desk_query\"\n" +
                    "</script>");
        }

    }
    @RequestMapping("/desk_changeState/{id}/{state}")
    public void changeState(@PathVariable("id") int id,@PathVariable("state") int state,HttpServletResponse response) throws Exception{
        response.setContentType("text/html;charset=utf-8");
        Desk desk=new Desk();
        desk.setId(id);
        desk.setState(state);
        if(state==1){
            desk.setBooktime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }else{
            desk.setBooktime(null);
        }
        int count = deskService.changeState(desk);
        String message="";
        if (count > 0) {
            if(state==1){
                message="餐桌预定成功！";
            }else{
                message="餐桌退桌成功！";
            }
            response.getWriter().write("<script>alert('"+message+"');location.href='/desk_query';</script>");
        }else{
            if(state==1){
                message="餐桌预定失败！";
            }else{
                message="餐桌退桌失败！";
            }
            response.getWriter().write("<script>alert('"+message+"');location.href='/desk_query';</script>");
        }
    }

    /*
    * 进入前台点餐系统
    * */
    @RequestMapping({"/main","/"})
    public String getDesks(Model model){
        List<Desk> desks = deskService.getDesks(null);
        model.addAttribute("desks",desks);
        return "main";
    }

}
